const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showTopTips: false,
    name: '',
    imageList: [],
    errmsg: '错误',
    doing: false,
    data: {},
    teamNum: -1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    app.login().then(function() {
      console.log(app.globalData.data)
      that.setData({
        teamPre: app.globalData.teamPre,
        data: app.globalData.data,
      })

      var t = [];
      if (that.data.isTeamed) {
        return
      }
      for (let x in that.data.teamPre) {
        var team = that.data.teamPre[x][1].split(' ')
        for (let y in team) {
          if (team[y] == that.data.data.name) {
            that.setData({
              teamNum: parseInt(x) + 1
            })
            if (that.data.teamPre[x][3].length > 0 && that.data.teamPre[x][3] != that.data.data.name) {
              //已经有队长了，那你不是队长~
              let cookie = wx.getStorageSync('cookieKey');
              let header = {};
              if (cookie) {
                header.Cookie = cookie;
              }
              wx.request({
                url: app.globalData.url + '/wxapp/create_team',
                header: header,
                data: {
                  teamNum: that.data.teamNum,
                },
                success: function(res) {
                  console.log(res)
                  wx.showToast({
                    title: res.data,
                    duration: 3000
                  })
                  wx.navigateTo({
                    url: '../index/index',
                  })
                  wx.switchTab({
                    url: '../index/index',
                  })
                },
                fail: function(res) {
                  console.log(res)
                }
              })

            }
          }
        }
      }
      that.setData({
        teamAfter: t
      })
    })

  },
  close: function() {
    wx.switchTab({
      url: '../index/index',
    })
  },

  showTopTips: function() {
    var that = this;
    this.setData({
      showTopTips: true
    });
    setTimeout(function() {
      that.setData({
        showTopTips: false
      });
    }, 3000);
  },
  submit: function() {
    let that = this;
    if (that.data.name.length >= 2 && that.data.name.length <= 10 && !that.data.doing) {
      console.log(that.data.name)
      that.setData({
        doing: true
      })
      if (that.data.imageList.length > 0) {
        console.log('提交数据')
        console.log(that.data.imageList[0])
        let cookie = wx.getStorageSync('cookieKey');
        let header = {};
        if (cookie) {
          header.Cookie = cookie;
        }
        //把图片传上去 顺便把组名传上去
        wx.uploadFile({
          url: app.globalData.url + '/wxapp/create_team',
          filePath: that.data.imageList[0],
          name: that.data.name,
          header: header,
          formData: {
            'teamNum': that.data.teamNum,

          },
          success: function(res) {
            console.log(res);
            wx.showToast({
              title: "队伍创建成功！",
              duration: 3000
            })
            wx.navigateTo({
              url: '../index/index',
            })
            wx.switchTab({
              url: '../index/index',
            })
          },
          fail: function(res) {
            console.log(res);
            wx.showToast({
              title: res
            })
            that.setData({
              doing: false
            })
          }
        })
      } else {
        that.setData({
          errmsg: '请选择队伍头像！',
          doing: false
        })
        that.showTopTips()

      }
    } else {
      that.setData({
        errmsg: '请输入正确的队伍名称！',
        doing: false
      })
      that.showTopTips()
    }
  },
  close: function() {
    wx.navigateTo({
      url: '../index/index',
    })
  },
  bindKeyInput: function(e) {
    this.setData({
      name: e.detail.value
    })
    console.log(e.detail.value)
  },
  chooseImage: function() {
    var that = this
    wx.chooseImage({
      sizeType: 'compressed',
      count: 1,
      success: function(res) {
        console.log(res)
        that.setData({
          imageList: res.tempFilePaths
        })
      }
    })
  },
  previewImage: function(e) {
    var current = e.target.dataset.src

    wx.previewImage({
      current: current,
      urls: this.data.imageList
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})